<?php require_once('../../core/import.php');?>
<?php User::Permission('NEWS_UPDATE'); ?>
<?php
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
         #图片处理
        $Result=Upload::Probe($_FILES['Thumbnail'],Config::UPLOAD_ATTACHMENT_SIZE_IMAGE,Config::UPLOAD_ATTACHMENT_EXT_IMAGE,Config::PATH_UPLOAD_ATTACHMENT_NEWS);
        
        #如果重新上传了图片，处理
        $FileName=NULL;
        if($Result['error']!=UPLOAD_ERR_NO_FILE)    //如果上传了文件
        {
            switch($Result['error'])
            {
                case UPLOAD_ERR_OK:
                    $FileName=Upload::Execute($_FILES['Thumbnail'],Config::PATH_UPLOAD_ATTACHMENT_NEWS);
                break;
                case Upload::UPLOAD_ERR_CUSTOM_EXT:
                    exit('{"Number":"2","Description":"缩略图格式不正确，只能是：'.join(',',Upload::$UPLOAD_EXT_IMAGE).'。"}');
                break;
                case Upload::UPLOAD_ERR_CUSTOM_SIZE:
                    exit('{"Number":"3","Description":"缩略图大小超过了限制，不能超过：'.Functions::SizeFormat(Config::UPLOAD_ATTACHMENT_SIZE_IMAGE).'。"}');
                break;
                default :
                    exit('{"Number":"4","Description":"缩略图上传失败。错误代码：'.$Result['error'].'，错误描述：'.$Result['message'].'。"}');
            }
        }
	  
        #上传数据
	  $Data=array
		(
			'ClassID'=>Functions::FormArgs('FirstClass'),
			'SubclassID'=>Functions::FormArgs('SecondClass'),
			'Title'=>Functions::FormArgs('Title'),
			'Content'=>Functions::FormArgs('Content',NULL,FALSE),
			'Auhtor'=>Functions::FormArgs('Auhtor'),
			'Origin'=>Functions::FormArgs('Origin'),
			'Keywords'=>Functions::FormArgs('Keywords'),
			'Summary'=>Functions::FormArgs('Summary'),
			'M_IsDelete'=>0,
			'M_Count'=>0
		);
		
		$News_Top=DB::GetRows('select M_IsHeadline,M_IsRecommend,M_IsTop from `news` where ID='.Functions::FormArgs('ID').'');
		if(Functions::FormArgs('M_IsHeadline')!=$News_Top[0]['M_IsHeadline']) 
		{
			$Data['M_IsHeadline']=Functions::FormArgs('M_IsHeadline');
			$Data['M_IsHeadline_Datetime']=date('Y-m-d G:i:s');
		}
		if(Functions::FormArgs('M_IsRecommend')!=$News_Top[0]['M_IsRecommend']) 
		{
			$Data['M_IsRecommend']=Functions::FormArgs('M_IsRecommend');
			$Data['M_IsRecommend_Datetime']=date('Y-m-d G:i:s');
		}
		if(Functions::FormArgs('M_IsTop')!=$News_Top[0]['M_IsTop']) 
		{
			$Data['M_IsTop']=(string)Functions::FormArgs('M_IsTop');
			$Data['M_IsTop_Datetime']=date('Y-m-d G:i:s');
		}
		if($FileName)
		{
			$Data['Thumbnail']=$FileName ;
		}

        DB::UpdateRowByID('news',Functions::FormArgs('ID'),$Data);
		#相关链接
		$oc_fileCounter=Functions::FormArgs("oc_fileCounter");
		if($oc_fileCounter>0){
			for($i=1;$i<=$oc_fileCounter;$i++)
				{
					$News_Title=Functions::FormArgs('news_title'.$i);
					$News_Url=Functions::FormArgs('news_url'.$i);
					if($News_Title && $News_Url)
					{
						$Link=array
						(
							'NewsID'=>Functions::FormArgs('ID'),
							'Datetime'=>date('Y-m-d G:i:s')
						);
						if($News_Title){
							$Link['Title']=$News_Title;
						}
						if($News_Url){
							$Link['Link']=$News_Url;
						}
						DB::Insert('news_link',$Link);
					}
				}
		}
        echo '{"Number":"0","Description":"成功执行"}';
    }
?>